// Bootstrap Mid-Small - col-ms-* - the missing grid set for Bootstrap3.
//
// This is a hack to fill the gap between 480 and 767 pixels - a missing range
// in the bootstrap responsive grid structure. Use these classes to style pages
// on cellphones when they transition from portrait to landscape.
//
// Contains:
// Columns, Offsets, Pushes, Pulls for the Mid-Small layout
// Visibility classes for the Mid-Small layout
// Redefined visibility classes for the Extra Small layout
//
// See https://github.com/twbs/bootstrap/issues/10203 for more info.
// Forked from: https://gist.github.com/Jakobud/8eca95f07a3b50453cd7
// Original gist: https://gist.github.com/andyl/6360906
//
// Instructions: Add the following to the end of bootstrap.less
//
// @import "bootstrap-ms";
//

// Mid-Small breakpoint

@grid-columns:              12;
@grid-gutter-width:         30px;
.responsive-visibility() {
  display: block !important;
  table&  { display: table !important; }
  tr&     { display: table-row !important; }
  th&,
  td&     { display: table-cell !important; }
}

.responsive-invisibility() {
  display: none !important;
}

.make-grid-columns() {
  // Common styles for all sizes of grid columns, widths 1-12
  .col(@index) { // initial
    @item: ~".col-xs-@{index}, .col-sm-@{index}, .col-md-@{index}, .col-lg-@{index}";
    .col((@index + 1), @item);
  }
  .col(@index, @list) when (@index =< @grid-columns) { // general; "=<" isn't a typo
    @item: ~".col-xs-@{index}, .col-sm-@{index}, .col-md-@{index}, .col-lg-@{index}";
    .col((@index + 1), ~"@{list}, @{item}");
  }
  .col(@index, @list) when (@index > @grid-columns) { // terminal
    @{list} {
      position: relative;
      // Prevent columns from collapsing when empty
      min-height: 1px;
      // Inner gutter via padding
      padding-left:  ceil((@grid-gutter-width / 2));
      padding-right: floor((@grid-gutter-width / 2));
    }
  }
  .col(1); // kickstart it
}

.float-grid-columns(@class) {
  .col(@index) { // initial
    @item: ~".col-@{class}-@{index}";
    .col((@index + 1), @item);
  }
  .col(@index, @list) when (@index =< @grid-columns) { // general
    @item: ~".col-@{class}-@{index}";
    .col((@index + 1), ~"@{list}, @{item}");
  }
  .col(@index, @list) when (@index > @grid-columns) { // terminal
    @{list} {
      float: left;
    }
  }
  .col(1); // kickstart it
}

.calc-grid-column(@index, @class, @type) when (@type = width) and (@index > 0) {
  .col-@{class}-@{index} {
    width: percentage((@index / @grid-columns));
  }
}
.calc-grid-column(@index, @class, @type) when (@type = push) and (@index > 0) {
  .col-@{class}-push-@{index} {
    left: percentage((@index / @grid-columns));
  }
}
.calc-grid-column(@index, @class, @type) when (@type = push) and (@index = 0) {
  .col-@{class}-push-0 {
    left: auto;
  }
}
.calc-grid-column(@index, @class, @type) when (@type = pull) and (@index > 0) {
  .col-@{class}-pull-@{index} {
    right: percentage((@index / @grid-columns));
  }
}
.calc-grid-column(@index, @class, @type) when (@type = pull) and (@index = 0) {
  .col-@{class}-pull-0 {
    right: auto;
  }
}
.calc-grid-column(@index, @class, @type) when (@type = offset) {
  .col-@{class}-offset-@{index} {
    margin-left: percentage((@index / @grid-columns));
  }
}

// Basic looping in LESS
.loop-grid-columns(@index, @class, @type) when (@index >= 0) {
  .calc-grid-column(@index, @class, @type);
  // next iteration
  .loop-grid-columns((@index - 1), @class, @type);
}

.make-grid(@class) {
  .float-grid-columns(@class);
  .loop-grid-columns(@grid-columns, @class, width);
  .loop-grid-columns(@grid-columns, @class, pull);
  .loop-grid-columns(@grid-columns, @class, push);
  .loop-grid-columns(@grid-columns, @class, offset);
}
@screen-sm:                  768px;
@screen-sm-min:              @screen-sm;
@screen-lg:                  1200px;
@screen-ml:                  1440px;
@screen-ml-min:              @screen-ml;
@screen-xl:                  1650px;
@screen-xl-min:              @screen-xl;
@screen-lg-max:              (@screen-ml-min - 1);
@screen-ml-max:              (@screen-xl-min - 1);
@screen-ms: 480px;
@screen-ms-min: @screen-ms;
@screen-ms-max: (@screen-sm-min - 1);

// Redefined Extra Small max value
@screen-xs-max-new: (@screen-ms-min - 1);

// Common styles (see make-grid-columns() in bootstrap/mixins/_grid-framework.less)

.col-ms-1,
.col-ms-2,
.col-ms-3,
.col-ms-4,
.col-ms-5,
.col-ms-6,
.col-ms-7,
.col-ms-8,
.col-ms-9,
.col-ms-10,
.col-ms-11,
.col-ms-12,
.col-xl-1,
.col-xl-2,
.col-xl-3,
.col-xl-4,
.col-xl-5,
.col-xl-6,
.col-xl-7,
.col-xl-8,
.col-xl-9,
.col-xl-10,
.col-xl-11,
.col-xl-12 {
    position: relative;
    // Prevent columns from collapsing when empty
    min-height: 1px;
    // Inner gutter via padding
    padding-left: (@grid-gutter-width / 2);
    padding-right: (@grid-gutter-width / 2);
    float: left;
}

// Misc. class adjustments

@media (min-width: @screen-ms) and (max-width: @screen-ms-max) {
    .container {
        max-width: @screen-sm - 20px;
    }
    .hidden-xs {
        display: block !important;
    }
}

@media (min-width: @screen-lg) and (max-width: @screen-lg-max) {
    .container {
        max-width: @screen-ml - 20px;
    }
    .hidden-xs {
        display: block !important;
    }
}

@media (min-width: @screen-ml) and (max-width: @screen-ml-max) {
    .container {
        max-width: @screen-xl - 20px;
    }
    .hidden-xs {
        display: block !important;
    }
}

// grid

@media (min-width: @screen-ms-min) and (max-width: @screen-ms-max) {
    .make-grid(ms);
}

@media (min-width: @screen-lg) and (max-width: @screen-lg-max) {
    .make-grid(lg);
}

@media (min-width: @screen-xl) {
    .make-grid(xl);
}

// Visibility utilities

.visible-xs, .visible-ms {
    // set to no display so we have a fresh start
    .responsive-invisibility;
}

.visible-xs-block,
.visible-xs-inline,
.visible-xs-inline-block,
.visible-ms-block,
.visible-ms-inline,
.visible-ms-inline-block {
    display: none !important;
}

@media (max-width: @screen-xs-max-new) {
    .visible-xs {
        .responsive-visibility;
    }
}
.visible-xs-block {
    @media (max-width: @screen-xs-max-new) {
        display: block !important;
    }
}
.visible-xs-inline {
    @media (max-width: @screen-xs-max-new) {
        display: inline !important;
    }
}
.visible-xs-inline-block {
    @media (max-width: @screen-xs-max-new) {
        display: inline-block !important;
    }
}

@media (min-width: @screen-ms-min) and (max-width: @screen-ms-max) {
    .visible-ms {
        .responsive-visibility;
    }
}
.visible-ms-block {
    @media (min-width: @screen-ms-min) and (max-width: @screen-ms-max) {
        display: block !important;
    }
}
.visible-ms-inline {
    @media (min-width: @screen-ms-min) and (max-width: @screen-ms-max) {
        display: inline !important;
    }
}
.visible-ms-inline-block {
    @media (min-width: @screen-ms-min) and (max-width: @screen-ms-max) {
        display: inline-block !important;
    }
}

@media (max-width: @screen-xs-max-new) {
    .hidden-xs {
        .responsive-invisibility;
    }
}

@media (min-width: @screen-ms-min) and (max-width: @screen-ms-max) {
    .hidden-ms {
        .responsive-invisibility;
    }
}